using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 可挂载在 UI 元素（标题栏或拖动区域）上，实现用鼠标拖动整个窗口。
/// 需要一个 WindowController 引用（可以在 Inspector 指定或自动查找父物体）。
/// </summary>
public class Handle : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    [Tooltip("可选，自动查找父对象的 WindowController")]
    public WindowController windowController;

    void Reset()
    {
        if (windowController == null)
            windowController = GetComponentInParent<WindowController>();
    }

    void Awake()
    {
        if (windowController == null)
            windowController = GetComponentInParent<WindowController>();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (windowController == null) return;
        if (eventData.button != PointerEventData.InputButton.Left) return;
        windowController.BeginWindowDrag(eventData.position);
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (windowController == null) return;
        windowController.DragWindowTo(eventData.position);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (windowController == null) return;
        windowController.EndWindowDrag();
    }
}